package com.ysg.widget.tree;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.ysg.R;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 支持选中
 */
public class TreeAdapter extends AbsTreeAdapter {
    public Map<String, NodeCheckEnum> checkMap = new LinkedHashMap<>();
    private boolean single = true;
    private boolean hasCheck = false;

    /**
     * 没有复选框用这个
     */
    public TreeAdapter(ListView listView, Context context, List<Node> trees, int defaultExpandLevel) {
        super(listView, context, trees, defaultExpandLevel);
    }

    /**
     * 有复选框用这个
     */
    public TreeAdapter(ListView listView, Context context, List<Node> trees, int defaultExpandLevel, Map<String, NodeCheckEnum> checkMap, boolean single) {
        super(listView, context, trees, defaultExpandLevel);
        this.hasCheck = true;
        this.single = single;
        if(null != checkMap && !checkMap.isEmpty()){
            this.checkMap.putAll(checkMap);
        }
    }

    /**
     * 数据有变化用这个
     */
    public void setNewData(List<Node> nodes){
        mAllNodes = TreeHelper.getSortedNodes(nodes, 0);
        mNodes = TreeHelper.filterVisibleNode(mAllNodes);
        notifyDataSetChanged();
    }

    @Override
    public View getConvertView(Node node, int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_tree, parent, false);
        ImageView icon = convertView.findViewById(R.id.tree_icon);
        ImageView ivCheck = convertView.findViewById(R.id.tree_check);
        TextView tvName = convertView.findViewById(R.id.tree_name);

        tvName.setText(node.getName());
        //图标
        setIcon(node, icon);
        //复选框
        setCheckBox(node, ivCheck);
        return convertView;
    }

    /**
     * 设置图标
     */
    private void setIcon(Node node, ImageView icon) {
        if (node.isLeaf()) {
            icon.setVisibility(View.INVISIBLE);
        } else {
            icon.setVisibility(View.VISIBLE);
            if (node.isExpand()) {
                icon.animate().rotation(45).setDuration(0).start();
            } else {
                icon.animate().rotation(0).setDuration(0).start();
            }
        }
    }

    /**
     * 设置复选框
     */
    private void setCheckBox(Node node, ImageView ivCheck) {
        ivCheck.setVisibility(hasCheck? View.VISIBLE: View.GONE);
        if (hasCheck){
            ivCheck.setVisibility(View.VISIBLE);
            if (single && !node.isLeaf()){
                ivCheck.setVisibility(View.GONE);
            }
        }else{
            ivCheck.setVisibility(View.GONE);
        }

        NodeCheckEnum checkState = checkMap.get(node.getId());
        if(null != checkState){
            switch (checkState) {
                case UNCHECK:
                    ivCheck.setImageResource(R.mipmap.ic_tree_uncheck);
                    break;
                case CHECK:
                    ivCheck.setImageResource(R.mipmap.ic_tree_check);
                    break;
                case PART_CHECK:
                    ivCheck.setImageResource(R.mipmap.ic_tree_part_check);
                    break;
            }
        }else{
            ivCheck.setImageResource(R.mipmap.ic_tree_uncheck);
        }

        ivCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NodeCheckEnum checked = checkMap.get(node.getId());
                if (single){
                    if(NodeCheckEnum.CHECK == checked){
                        setSingle(false, node.getId());
                    }else{
                        setSingle(true, node.getId());
                    }
                }else{
                    if(NodeCheckEnum.CHECK == checked){
                        setMultiple(false, node);
                    }else{
                        setMultiple(true, node);
                    }
                }
            }
        });
    }

    //单选
    private void setSingle(boolean checked, String id) {
        checkMap.clear();
        if(checked){
            checkMap.put(id, NodeCheckEnum.CHECK);
        }
        notifyDataSetChanged();
    }

    //多选
    private void setMultiple(boolean checked, Node node) {
        if (checked){
            //1.自己选中
            checkMap.put(node.getId(), NodeCheckEnum.CHECK);
            //2.是否有子，有：遍历子选中
            setChildrenCheck(node);
            //3.是否有父，有：遍历父部分选中或选中
            setParentCheck(node);
        }else{
            //1.自己不选中
            checkMap.put(node.getId(), NodeCheckEnum.UNCHECK);
            //2.是否有子，有：遍历子不选中
            setChildrenUncheck(node);
            //3.是否有父，有：遍历父不选中或部分选中
            setParentUnCheck(node);
        }
        notifyDataSetChanged();
    }

    /**
     * 是否有父，有：遍历父不选中或部分选中
     */
    private void setParentUnCheck(Node node) {
        Node pNode = node.getParent();
        if (pNode != null){
            List<Node> children = pNode.getChildren();
            if (children != null && children.size() > 0){
                checkMap.put(pNode.getId(), NodeCheckEnum.UNCHECK);
                boolean flag = false;
                for (Node n: children){
                    NodeCheckEnum checked =  checkMap.get(n.getId());
                    if (NodeCheckEnum.CHECK == checked || NodeCheckEnum.PART_CHECK == checked){
                        flag = true;
                        break;
                    }
                }
                if (flag){
                    checkMap.put(pNode.getId(), NodeCheckEnum.PART_CHECK);
                }
            }
            setParentUnCheck(pNode);
        }
    }

    /**
     * 是否有父，有：遍历父部分选中或选中
     */
    private void setParentCheck(Node node) {
        Node pNode = node.getParent();
        if (pNode != null){
            List<Node> children = pNode.getChildren();
            if (children != null && children.size() > 0){
                checkMap.put(pNode.getId(), NodeCheckEnum.CHECK);
                boolean flag = false;
                for (Node n: children){
                    NodeCheckEnum checked =  checkMap.get(n.getId());
                    if (NodeCheckEnum.CHECK != checked){
                        flag = true;
                        break;
                    }
                }
                if (flag){
                    checkMap.put(pNode.getId(), NodeCheckEnum.PART_CHECK);
                }
            }
            setParentCheck(pNode);
        }
    }

    /**
     * 是否有子，有：遍历子不选中
     */
    private void setChildrenUncheck(Node node) {
        List<Node> children = node.getChildren();
        if (children != null && children.size() > 0){
            for (Node n: children){
                checkMap.put(n.getId(), NodeCheckEnum.UNCHECK);
                setChildrenUncheck(n);
            }
        }
    }

    /**
     * 是否有子，有：遍历子选中
     */
    private void setChildrenCheck(Node node) {
        List<Node> children = node.getChildren();
        if (children != null && children.size() > 0){
            for (Node n: children){
                checkMap.put(n.getId(), NodeCheckEnum.CHECK);
                setChildrenCheck(n);
            }
        }
    }
}
